<template>
	<view>
		<view class="">
			<u-card :title="'发帖人：' + postsData.creatorName"
			title-color="#18b566"
			:border="true" :foot-border-top="false">
				<view class="" slot="body">
					<view class="u-body-item u-flex u-border-bottom u-col-between u-p-t-0">
						<view class="u-body-item-title">内容：{{postsData.content}}</view>
					</view>
				</view>
				<view class="" slot="body" style="margin-top: 50rpx;">
					<view class="reply" style="color: #18b566;">留言输入</view>
					<u-input type="textarea" :border="true" v-model="commentData.content"/>
					<view class="reply" style="color: #5677fc;float: right;margin-top: 20rpx;margin-bottom: 20rpx;">
						<u-button type="success" shape="square" size="mini" @click="toCmoments">提交留言</u-button>
					</view>
				</view>
			</u-card>
		</view>
		<u-divider style="background-color: rgb(241, 243, 248)">留言详情</u-divider>
		<view class="comment" v-for="(res, index) in commentList" :key="res.id">
			<!-- <view class="left"><image :src="res.url" mode="aspectFill"></image></view> -->
			<view class="right">
				<view class="top">
					<view class="name">{{ res.creatorName }}</view>
				</view>
				<view class="content">{{ res.content }}</view>
				<view class="reply-box">
					<view class="item" v-for="(item, index) in res.children" :key="item.index">
						<view class="username">{{ item.creatorName }} 回复 {{ res.creatorName }}</view>
						<view class="text">{{ item.content }}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getPostsDetail
	} from '@/api/posts/posts.js'
	import {
		saveComments,
		getCommentsByPostsId
	} from '@/api/posts/comments.js'
export default {
	data() {
		return {
			commentData: {
				pId: 0,
				postsId: '',
				content: '',
			},
			postsData: {},
			commentList: []
		};
	},
	onLoad(option) {
		this.commentData.postsId = option.id;
		this.getPostsDetail(option.id);
		this.getCommentsByPostsId(option.id);
	},
	methods: {
		// 根据帖子ID获取相关的留言
		getCommentsByPostsId(postsId) {
			getCommentsByPostsId(postsId).then(res => {
				this.commentList = res.data
			})
		},
		// 留言提交
		toCmoments() {
			saveComments(this.commentData).then(res => {
				if (res.code == 0) {
					this.commentData.content = ''
					uni.showToast({
						title: '评论成功！',
						icon: 'success'
					});
					this.getCommentsByPostsId(this.commentData.postsId)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'error'
					});
				}
			})
		},
		// 查询帖子详情
		getPostsDetail(id) {
			getPostsDetail(id).then(res => {
				this.postsData = res.data
			})
		}
	}
};
</script>

<style lang="scss" scoped>
.comment {
	display: flex;
	padding: 30rpx;
	.left {
		image {
			width: 64rpx;
			height: 64rpx;
			border-radius: 50%;
			background-color: #f2f2f2;
		}
	}
	.right {
		flex: 1;
		padding-left: 20rpx;
		font-size: 30rpx;
		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 10rpx;
			.name {
				color: #5677fc;
			}
			.like {
				display: flex;
				align-items: center;
				color: #9a9a9a;
				font-size: 26rpx;
				.num {
					margin-right: 4rpx;
					color: #9a9a9a;
				}
			}
			.highlight {
				color: #5677fc;
				.num {
					color: #5677fc;
				}
			}
		}
		.content {
			margin-bottom: 10rpx;
		}
		.reply-box {
			background-color: rgb(242, 242, 242);
			border-radius: 12rpx;
			.item {
				padding: 20rpx;
				border-bottom: solid 2rpx $u-border-color;
				.username {
					font-size: 24rpx;
					color: #999999;
				}
			}
			.all-reply {
				padding: 20rpx;
				display: flex;
				color: #5677fc;
				align-items: center;
				.more {
					margin-left: 6rpx;
				}
			}
		}
		.bottom {
			margin-top: 20rpx;
			display: flex;
			font-size: 24rpx;
			color: #9a9a9a;
			.reply {
				color: #5677fc;
				margin-left: 10rpx;
			}
		}
	}
}
</style>
